package main

import (
	"fmt"
	"golang.org/x/crypto/ssh"
	"io"
	"log"
)

func main() {
	// 读取私钥文件
	/*key, err := os.ReadFile("/Users/alias/.ssh/test")
	if err != nil {
		log.Fatalf("日志含义读取私钥文件失败: %s", err)
	}*/

	rsaPriKey := `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyRRPGAXOC0Y6DvBhujf5hyku77K/N2mJVCYzky9uApa0Jwy1
2AMXzy8cwTHMaEzR7s654DfzLe5h4ENdpDODL7Q7RI9567TQI9SfTLOJ05Wqn9Tu
DyZm+j8HrggUScwl1ES9/s+4J2pP/rrALIjbteqIoN9iVx0AXwl6T/jm6/bmDZAE
vGl2FAXKOrF4hRr6oJtm791yJ2nB7O5yOAIHZPezLLEPOl/suCYFeEpafR2jUG2w
P74U/sJEdUiDdt3+k+r3romwyyf1gIZTo2DnI4LON8Ob5CPM5g+T3zkVodVsb0xG
IchG+GzfXglq/3MFalDoYSzhTMYuA1ZcpYHQvwIDAQABAoIBAQCX8gvOmFxZsqie
r1D5+eBKHXnFUaojAlHuz6s7dqTHuf/KXlVvNO8h9XTiHHJdAmfE8FwPLdhKBg/Z
+q3hLbfdLXF33n9/+LMr0DjMKgNlanxzqOB3fEpmoYh5HsvN9EFFxOhZi7eZuUFX
uOENBiIYtnCmOAdE0rp8Flph4jEFbsWLCO7L7OyOVBgl8LrXJiivfIFDBMoaei4/
iLsZ6l4nTs8bYnXqwwedVUkkLVZ9Cx7cvr05/P4DWoxa442Cbhs0SP2cSLNYEIZs
svXXxq6ruqGdN60r35XXuMtnRykZmLB58auCsx4aHTswHVuwowP/eqzwzHOl9HFo
g5Fq2g7ZAoGBAPj2GhKVJpJbRxHs2/9TCO+x1Tg3jVhgEsjCfla10tl2YwuCUbtL
lX6OCcJUW+aWp98vW50j0D7KEpdyuDpcBkoTMZcL7F0Czqb+6MEovjS7+JvlBzBQ
Uvsvd1GnFpXUhuRqh7+DD74St4Aw/EoEySyedFC4Ep3o4GeJ2JlWf8IFAoGBAM7D
p0BwLlwVK0ND8MDGlbyuc41jWtzSGx/YFJt3Z2KRTkXHQsAVg60MHlkkzCP7C9eY
9u+WQ9K4S0oKjPmjFkll08HNKV2u3WP2hdOjvo/233+0FUNjaASZMY3XP6BZaBkv
az/a90k9vCr5dW0KXTv83lUSeM1zP/0BgvavLO7zAoGBAKIKjK3Lpm9cOcXRBOAK
i6Z3ePA+OCFtZsyEyvZlIfeoto6nI8tkRsk4aeGyRp6vMFRz3o1I4iH8JF2urXpO
q0lLdNt7RzzCtv66q4QNoL9yGT5es28e2OPk4SSkPG8JBUNmkzRzeaudQxKMz6jd
tJL2D376UCO3j1fUwMYNJtqBAoGAItVhewCrPsYuOLHcgI20MkptmzdO2n7nJPFB
X4O5Pijet/C+9R9qxJ+TCU+iuLkVoKfbijeVi42S2ZF3eKMaIRlvmGnY+zh3gdzJ
ZQNkGlrsAlazclvv7T5l09CcnoYRfJS5i9N277bf2ZmXR9yEIpQSXOC0y2AMOrKj
fq26czMCgYASNhQniZMXGUG9aKYfuwdN2hF9hgTGOB5LOurJVt0vbzROKyYVdtHG
oKUfqVMlCg0UgUDlTICFLv4TfdFNWEk5e3gcIISyK3bcYFXnBp0Xgfc/FKs3XmkT
Ve4UV1tBPU/aszv+E2sw72QMTL1GciSUBVfV+L4IH5mXVRi4BC4Cbw==
-----END RSA PRIVATE KEY-----
`

	// 解析私钥文件
	signer, err := ssh.ParsePrivateKey([]byte(rsaPriKey))
	if err != nil {
		log.Fatalf("解析私钥失败: %s", err)
	}

	// 远程主机 SSH 连接配置
	config := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}

	// 连接远程主机
	conn, err := ssh.Dial("tcp", "47.95.203.124:22", config)
	if err != nil {
		log.Fatalf("ssh握手失败: %s", err)
	}
	defer conn.Close()

	// 创建 SSH 会话
	session, err := conn.NewSession()
	if err != nil {
		log.Fatalf(" 创建 SSH 会话失败: %s", err)
	}
	defer session.Close()

	// 执行命令
	stdout, err := session.StdoutPipe()
	if err != nil {
		log.Fatalf("执行命令失败: %s", err)
	}

	err = session.Start("ls -l && echo 'hello world' && sleep 5 && ls -l")
	if err != nil {
		log.Fatalf("Failed to start command: %s", err)
	}

	// 实时获取输出流并输出到标准输出
	go func() {
		for {
			buf := make([]byte, 1024)
			n, err := stdout.Read(buf)
			if err != nil {
				if err != io.EOF {
					log.Fatalf("Failed to read stdout: %s", err)
				}
				break
			}
			fmt.Print(string(buf[:n]))
		}
	}()

	// 等待命令执行完毕
	err = session.Wait()
	if err != nil {
		log.Fatalf("Command execution failed: %s", err)
	}
}
